package kr.couponplay.android.activity;

import java.util.ArrayList;
import java.util.List;

import kr.couponplay.android.AppContext;
import kr.couponplay.android.activity.deal.DealListActivity;
import kr.couponplay.android.activity.event.EventActivity;
import kr.couponplay.android.activity.info.InfoActivity;
import kr.couponplay.android.activity.message.MessageActivity;
import kr.couponplay.android.activity.recent.RecentActivity;
import kr.couponplay.android.activity.search.SearchActivity;
import kr.couponplay.android.support.DisplayUtils;
import android.app.TabActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MainTabActivity extends TabActivity {
	private List<TabMenuResource> tabMenuResources;
	
	private void initTabActivity() {
		tabMenuResources = new ArrayList<TabMenuResource>() {{
			add(new TabMenuResource(getResources().getString(R.string.app_name), R.drawable.button_m_all, DealListActivity.class));
			if (AppContext.doEvent()) {
				add(new TabMenuResource(AppContext.getEventName(getApplicationContext()), R.drawable.button_m_event, EventActivity.class));
			}
			add(new TabMenuResource(getResources().getString(R.string.menu_recent), R.drawable.button_m_recent, RecentActivity.class));
			add(new TabMenuResource(getResources().getString(R.string.menu_message), R.drawable.button_m_message, MessageActivity.class));
			add(new TabMenuResource(getResources().getString(R.string.menu_search), R.drawable.button_m_search, SearchActivity.class));
		}};
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
		setContentView(R.layout.tab_host_activity);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tab_title_frame);
		
		initTabActivity();
		
		TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();
        TitleBarHelper.setFrameLayout((FrameLayout)findViewById(R.id.title_layout));
        
		for (int i=0; i< tabMenuResources.size(); i++) {
			View view = LayoutInflater.from(tabHost.getContext()).inflate(R.layout.tab_widget, null);
			drawTabMenuButton(view, i);
			
			TabSpec tabSpec = tabHost.newTabSpec("tab_menu_" + i).setIndicator(view);
			tabSpec.setContent(new Intent(this, tabMenuResources.get(i).getClazz()));
			tabHost.addTab(tabSpec);
		}
		TitleBarHelper.setTabHost(tabHost);
	}
	
	private void drawTabMenuButton(View view, int position) {
		TabMenuResource tabMenuResource = tabMenuResources.get(position);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f);
		params.setMargins(0, DisplayUtils.pixelFromDp(this, 3), 0, 0);

		TextView tabMenu = (TextView)view.findViewById(R.id.tab_menu_text);
		tabMenu.setText(tabMenuResource.getName());
		tabMenu.setCompoundDrawablesWithIntrinsicBounds(0, tabMenuResource.getImgResourceId(), 0, 0);
		tabMenu.setLayoutParams(params);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.option_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.option_menu_1) {
			Intent intent = new Intent(this, InfoActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
			startActivity(intent);
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
		} else if (item.getItemId() == R.id.option_menu_2) {
			startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+getApplicationContext().getPackageName())));//http://tsto.re/0000319472"
		} else if (item.getItemId() == R.id.option_menu_3) {
			finish();
			CacheHelper.clear(this);
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		return true;
	}

	private class TabMenuResource {
		private String name;
		private int imgResourceId;
		private Class<?> clazz;
		
		public TabMenuResource(String name, int imgResourceId, Class<?> clazz) {
			this.name = name;
			this.imgResourceId = imgResourceId;
			this.clazz = clazz;
		}

		public Class<?> getClazz() {
			return clazz;
		}
		public String getName() {
			return name;
		}
		public int getImgResourceId() {
			return imgResourceId;
		}
	}
}
